package linter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import linter.POJO.DO.Comments;
import linter.service.CommentsService;
import linter.mapper.CommentsMapper;
import linter.utils.PropertyUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author 北煜
* @description 针对表【comments(评论情况表)】的数据库操作Service实现
* @createDate 2024-03-15 11:17:37
*/
@Service
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments>
    implements CommentsService{

    @Resource
    CommentsMapper commentsMapper;

    @Override
    public String insertComments(Long uuid, Integer postsId, Integer parentContentId,Object content) {
        Date date = new Date();
        Comments comments = new Comments();
        comments.setUuid(uuid);
        comments.setPostsid(postsId);
        comments.setParentContentId(parentContentId);
        comments.setContent(content);
        comments.setCreateTime(date);
        comments.setUpdateTime(date);
        int flag = commentsMapper.insert(PropertyUtils.doToDto(comments, Comments.class));
        if (flag != 1) { return "评论失败"; }
        return "评论成功";
    }

    @Override
    public String updateComments(Long uuid, Integer postsId, Integer commentsId, Object content) {
        Date date = new Date();
        Comments comments = new Comments();
        QueryWrapper<Comments> queryWrapper = new QueryWrapper<>();
        comments.setUpdateTime(date);
        comments.setContent(content);
        int flag = commentsMapper.update(comments, queryWrapper
                .eq("uuid", uuid).eq("postsId", postsId).eq("id", commentsId));
        if (flag != 1) { return "修改评论失败"; }
        return "修改评论成功";
    }

    @Override
    public String deleteComments(Long uuid, Integer postsId, Integer commentsId) {
        Date date = new Date();
        Comments comments = new Comments();
        QueryWrapper<Comments> queryWrapper = new QueryWrapper<>();
        comments.setUpdateTime(date);
        comments.setIsDelete(1);
        int flag = commentsMapper.update(comments, queryWrapper
                .eq("uuid", uuid).eq("postsId", postsId).eq("id", commentsId));
        if (flag != 1) { return "删除评论失败"; }
        return "删除评论成功";
    }

    @Override
    public List<Comments> getComments(Integer postsId, Integer sort) {
        if (sort == 0){
            return commentsMapper.selectByPostsId(postsId);
        }
        return commentsMapper.selectByLikesNub(postsId);
    }

    @Override
    public boolean select(Integer commentsId) {
//        System.out.println(commentsMapper.selectByCommentsId(commentsId));
        return commentsMapper.selectByCommentsId(commentsId);
    }

    @Override
    public boolean likesComments(Integer commentsId) {
        Comments comments = new Comments();
        QueryWrapper<Comments> queryWrapper = new QueryWrapper<>();
        if (comments.getLikeNub() == null){ comments.setLikeNub(0); }
        comments.setLikeNub(comments.getLikeNub()+1);
        int flag = commentsMapper.update(comments, queryWrapper.eq("id", commentsId).eq("is_Delete", 0));
        if (flag != 1){ return false; }
        return true;
    }
}




